package nccloud.impl.ext;

import com.alibaba.fastjson.JSONObject;
import nc.bs.dao.BaseDAO;
import nc.vo.bd.psn.PsndocVO;
import nc.vo.pf.msg.MessageMetaVO;
import nc.vo.sm.UserVO;
import nccloud.itf.ext.ILcSSOUrlExt;
import pub.util.LCPubCommon;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import static pub.util.LCPubCommon.*;

/**
 * 单点地址
 */
public class LcSSOUrlExtImpl implements ILcSSOUrlExt {

    private BaseDAO dao = null;

    private BaseDAO getBaseDAO() {
        if (dao == null) {
            dao = new BaseDAO();
            return dao;
        }
        return dao;
    }

    @Override
    public JSONObject getSSOUrlInfo(String pk_wf_messagemeta) throws Exception {
        JSONObject result = new JSONObject();
//        try {
//
//            MessageMetaVO messageMetaVO = (MessageMetaVO) getBaseDAO().retrieveByPK(MessageMetaVO.class, pk_wf_messagemeta);
//
//
//            if (messageMetaVO == null) {
//                result.put("code", 500);
//                result.put("msg", "审批消息不存在！");
//                return result;
//            }
//
//            String receiver = messageMetaVO.getReceiver();
//
//            UserVO userVO = (UserVO) getBaseDAO().retrieveByPK(UserVO.class, receiver);
//
//            String pk_psndoc = userVO.getPk_psndoc();
//
//            PsndocVO psndocVO = (PsndocVO) getBaseDAO().retrieveByPK(PsndocVO.class, pk_psndoc);
//
//
//            //读取配置文件
//            String a = LCPubCommon.getFilePath();
//
//
//            String baseSSOUrl = LCPubCommon.getProperties("baseSSOUrl"); //"https://uat.maycur.com";
//
//            String ssoStr = "%s/sso?entCode=%s&userId=%s&timestamp=%s&token=%s&ssoMode=sso&language=zh&url=%s&embed=true&loginSrc=ngMsg&hideActionBar=true&isHideHeader=true&isHideApprovalLog=true&defaultTab=ATTACHMENTS&showTabs=AUDIT_REPORT,ATTACHMENTS";
//
//            //String ssoStr="https://www.maycur.com/sso?entCode=%s&userId=%s&timestamp=%s&token=%s&ssoMode=sso&language=zh&url=%s&embed=true&loginSrc=ngMsg&hideActionBar=true&isHideHeader=true&isHideApprovalLog=true&defaultTab=ATTACHMENTS&showTabs=AUDIT_REPORT,ATTACHMENTS";
//
//            String entCode = LCPubCommon.getProperties("entCode");
//            String ssoSecret =LCPubCommon.getProperties("ssoSecret");
//            String userId = psndocVO.getMobile();
//            String timestamp = getTimeStamp();
//            String token = getToken(ssoSecret + ":" + userId + ":" + timestamp);
//            String url = "/approve/REIMBURSE/null/264X2025072111867";
//
//
//            ssoStr = String.format(ssoStr, baseSSOUrl, entCode, userId, timestamp, token, url);
//
//
//            result.put("ssoStr", ssoStr);
//
//
//
//
//        } catch (Exception ex) {
//            throw new Exception(ex);
//        }

        return result;

    }


    private String getToken(String input) {
        String token = "";
        try {

            // 创建 SHA-256 消息摘要实例
            MessageDigest digest = MessageDigest.getInstance("SHA-256");

            // 计算 SHA-256 哈希值
            byte[] hashBytes = digest.digest(input.getBytes());

            // 将字节数组转换为十六进制字符串
            StringBuilder hexString = new StringBuilder();
            for (byte b : hashBytes) {
                hexString.append(String.format("%02x", b));
            }

            // 输出加密后的 SHA-256 哈希值
            System.out.println("SHA-256 加密结果：" + hexString.toString());
            token = hexString.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return token;
    }

    private String getTimeStamp() {
        long timestamp = System.currentTimeMillis();
        String timestampString = Long.toString(timestamp);
        return timestampString;
    }

}
